package cn.jcby.app.config;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.ArrayList;
import java.util.List;

//Spring boot方式
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
 
    /**
     * 分页插件
     */
    /*@Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }*/
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();

        List<ISqlParser> sqlParserList = new ArrayList<>();
        // 攻击 SQL 阻断解析器、加入解析链;防止小白或者恶意进行delete update 全表操作。注：若表配置使用了逻辑删除将正常执行删除。
        sqlParserList.add(new BlockAttackSqlParser());
        paginationInterceptor.setSqlParserList(sqlParserList);
        return paginationInterceptor ;
    }

    /**
     * 乐观锁插件:当要更新一条记录的时候，希望这条记录没有被别人更新
     * @return
     */
    /*@Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
        return new OptimisticLockerInterceptor() ;
    }*/


    @Bean
    public EasySqlInjector easySqlInjector () {
        return new EasySqlInjector();
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> {
            configuration.getTypeHandlerRegistry().register(new GeometryTypeHandler());
        };
    }
    @Bean
    public ConfigurationCustomizer configurationCustomizer1() {
        return configuration -> {
            configuration.getTypeHandlerRegistry().register(new PointTypeHandler());
        };
    }

  /*  @Bean
    public JtsModule jtsModule(){
        return new JtsModule();
    }

    @Bean
    public Jackson2ObjectMapperBuilder jacksonBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.modulesToInstall(new JtsModule());
        return builder;
    }*/
}